home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Language/OS - Multiplatform Resource Library
/
LANGUAGE OS.iso
/
oper_sys
/
choices
/
chcssml1.lha
/
Job.c
< prev
next >
Wrap
C/C++ Source or Header
|
1989-02-06
|
4KB
|
136 lines
/*
* This file is part of the Choices Operating System Simulator
* Developed by: The TAPESTRY Parallel Computing Laboratory
* University of Illinois at Urbana-Champaign
* Department of Computer Science
* 1304 W. Springfield Ave.
* Urbana, IL 61801
*
* Copyright (c) 1987, 1988, 1989 The University of Illinois Board of Trustees.
* All Rights Reserved.
* CONFIDENTIAL INFORMATION. Distribution restricted under license agreement.
*
* Author: Gary M. Johnston (johnston@cs.uiuc.edu)
* Project Manager and Principal Investigator: Roy Campbell (roy@cs.uiuc.edu)
*
* Funded by: NSF TAPESTRY Grant No. 1-5-30035, NASA ICLASS Grant
* No. 1-5-25469 and No. NSG1471 and AT&T Metronet Grant No. 1-5-37411.
*/
/*
* Job.c - Example work load.
*
* NOTE: Main() creates a Process whose entry point is Job(), below.
*/
#include <stdio.h>
#include "Choices.h"
#include "Name.h"
#include "Semaphore.h"
#include "Pipe.h"
/* Create semaphores I need and give em names to help debugging*/
Semaphore mutex_waitforsons(1, "mutex_waitforsons"),
waitforsons(0,"waitforsons");
int sons = 0;
int maxprocesses = 0;
Semaphore randmutex(1);
extern int rand();
int
random(int u)
{ //In the range 1..u
randmutex.P();
int r = rand();
if (r < 0) r = -r;
randmutex.V();
return 1 + r%u ;
};
struct Param
{ int id;
Pipe * lpipe, * rpipe;
};
int
Sample(void * arg )
{
Param * p = (Param *) arg;
int n = p->id;
Pipe * pipein = p->lpipe;
Pipe * pipeout = p->rpipe;
Assert(pipein != 0);
Assert(pipeout != 0);
for (int i = 0; i<15; i++)
{
Message * message;
int choose = random(4);
Print("\njob %d: [clock %d] iteration %d\n", n, clock, i);
if (choose <= 2)
{
Print("Choice send\n");
if (message == 0)
{ message = new Message;
message->value = i;
message->destination = random(maxprocesses);
message->source = n;
Print("\nAbout to send a message to %d contents %d\n",
message->destination,message->value);
}
else Print("\nSending Message on\n");
if (choose == 1) pipeout->sendAndBlock(message);
else pipeout->send(message);
}
if ( choose >=2 )
{
Print("\nChoice receive\n");
if (choose == 3 ) message=pipein->receiveAndBlock();
else message=pipein->receive();
if (message->destination==n)
{ Print("\nReceived a message to %d contents %d\n",
message->destination,message->value);
message = 0;
}
};
};
mutex_waitforsons.P();
/* decrement number of active sons. if zero sons
* wake up manager.
*/
if (--sons==0) waitforsons.V();
mutex_waitforsons.V();
Print("\nJob %d terminates\n", n);
return (0);
}
int
Job(void * argp)
{
maxprocesses = (int) argp;
Pipe * pipearray[20];
Print("Job Manager Creates %d user processes\n", maxprocesses);
for (int i = 0; i < maxprocesses; i++) pipearray[i] = new Pipe();
for (i = 0; i < maxprocesses; i++)
{
/* Create a user process */
Param * param = new Param;
param->id = i;
param->lpipe = pipearray[i];
param->rpipe = pipearray[(i+1)%maxprocesses];
Print("Address Pipe %d left. Pipe %d right\n",(int)param->lpipe, (int)param->rpipe);
Process * process =
new Process(Sample, (void *) param);
/* Put the user process on the ready queue */
ThisCPU()->getScheduler()->add( process );
mutex_waitforsons.P();
sons++;
mutex_waitforsons.V();
};
Print("\nNew Job Manager waits for sons\n");
waitforsons.P(); /* wait for sons to finish */
Print("\nNew Job Manager terminates\n");
return(0);
}